<style lang="less">
.phone{
	font-size: 40rpx;
	color: #333;
	font-weight: bold;
	margin: 400rpx auto 0;
}
.btn{
	width: 80%;
	height: 80rpx;
	background: rgba(255,0,0,0.7);
	color: #fff;
	font-size: 30rpx;
	border-radius: 50rpx;
	margin: 50rpx auto 0;
	font-weight: 500;
	letter-spacing: 1px;
	&:active{
		opacity: 0.8;
	}
}
.change{
	width: 90rpx;
	height: 90rpx;
	border-radius: 50%;
	background:#1890FF ;
	color: #FFFFFF;
	font-size: 35rpx;
	.icon-phone,.icon-check{
		font-size: 55rpx;
	}
	.icon-wechat2{
		font-size: 45rpx;
	}
}
.changename{
	font-size: 25rpx;
	color: #1890FF;
	font-weight: 500;
	margin: 10rpx 0 0;
}
.box_c{
	&:active{
		opacity: 0.8;
	}
}
.live_in{
	padding-top: 200rpx;
}
.live_btn{
	position: absolute;
	bottom: 100rpx;
	width: 100%;
}
.check_title{
font-size: 40rpx;
margin:50rpx 30rpx 30rpx;
font-weight: 500;
}
.check_con{
	margin: 0 35rpx;
	font-size: 30rpx;
	&>input{
		border-bottom: 1px solid #EDEDED;
		flex: 1;
		margin: 0 30rpx;
		padding: 10rpx 0;
		height: auto;
	}
}
.check_btn{
	width: 80%;
	height: 80rpx;
	background: rgba(255,0,0,0.7);
	color: #fff;
	font-size: 30rpx;
	border-radius: 50rpx;
	margin: 50rpx auto 0;
	font-weight: 500;
	letter-spacing: 1px;
	&:active{
		opacity: 0.8;
	}
}
.bind_phone{
	margin: 20rpx auto;
	color: rgba(255,0,0,0.7);
	font-size: 28rpx;
	text-decoration: underline;
}
</style>
<template>
	<view>
		<view class="status_bar status_bar_bak"></view>
		<!-- <view class="live_in" style="position: relative;" v-if="isMe">
			<view class="phone flex_center">
				{{userinfo.phone ? userinfo.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : '未绑定手机号，请先绑定手机号'}}
			</view>
			<view class="btn flex_center" @click="appIn">
				本机号码一键登录
			</view>
			<view class="bind_phone flex_center" @click="changeMe">
				绑定手机号
			</view>
		</view> -->
		<view class="live_in" v-if='bindTel'>
			<view class="getcheck">
				<view class="check_title">
					{{bindModel ? '绑定手机号' : '验证码登录'}}
				</view>
				<view class="check_con flex_v_venter">
					<text>输入手机号:</text>
					<input type="text" value="" placeholder="请输入手机号" placeholder-style="输入手机号" v-model='phone' />
				</view>
				<view class="check_btn flex_center" @click="toCheck" :style="phone.length == 11 && getReset < 1 ? '' : 'opacity: 0.4;' ">
					{{getReset ? getReset + 's后重新获取' : '获取验证码'}}
				</view>
			</view>
		</view>
		<view class="live_btn flex_center" v-if='bindWx'>
			<view class="flex_coum_center box_c" @click="wxLogin">
				<view class="change flex_center" style="">
					<i class='iconfont icon-wechat2'></i>
				</view>
				<view class="changename">
					{{bindModel ? '绑定微信' : '微信登录'}}
				</view>
			</view>
		</view>
		<!-- <view class="live_btn flex_center" v-else >
				<view class="flex_coum_center box_c" @click="changeMe">
					<view class="change flex_center" style="background: #4CD964;">
						<i class='iconfont icon-phone'></i>
					</view>
					<view class="changename" style="color: #4CD964;">
						本机登录
					</view>
				</view>
			</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {},
				isMe: true,
				phone: '',
				getReset: 0,
				timer: null,
				bindTel: true,
				bindWx: true,
				bindModel: false
			};
		},
		watch: {
			getReset(val) {
				if (this.timer) {
					clearInterval(this.timer)
				}
				if (val > 0) {
					this.timer = setInterval(() => {
						this.getReset -= 1
					}, 1000)
				} else {
					this.getReset = 0
				}
			}
		},
		onLoad(e) {
			console.log('befor: ',uni.getStorageSync('token'))
			if (e.bindModel) {
				this.bindTel = false
				this.bindWx = false
				this.bindModel = true
				if (e.bind === 'tel') {
					this.bindTel = true
				}
				if (e.bind === 'wx') {
					this.bindWx = true
				}
			}
		},
		methods: {
			wxLogin() {
				let _this = this
				plus.oauth.getServices(function(servies) {
					let s = servies[0]
					if (!s.authResult) {
						s.authorize(function(e) {
							uni.showLoading({
								title: '加载中',
								mask: true
							})
							_this.$ajax('user/wxLogin', {
								code: e.code
							}, 'post').then(res => {
								const item = res.data.obj
								console.log(item);
								let temp = ''
								if (item.update && item.task?.finish === 1) {
									temp = '?hasTaskComplate=true'
								}
								uni.setStorageSync('token', item.token)
								console.log('after: ',uni.getStorageSync('token'))
								uni.hideLoading()
								uni.reLaunch({
									url: `../index/index${temp}`
								})
							}).catch((err) => {
								console.log(err);
							})
						}, function(e) {
							uni.showModal({
								title: '认证失败',
								content: JSON.stringify(e),
							});
						}, {
							state: 'wechat_sdk_demo_test',
							appid: 'wx3db890de1a0f6efe'
						});
					}

				}, function(e) {
					console.log("获取服务列表失败：" + JSON.stringify(e));
				})
			},
			getToken() {
				this.$ajax('user/init', '', 'get').then(res => {
					uni.setStorageSync('token', res.data.obj.token)
					this.getInfo()
				})
			},
			changeMe() {
				this.isMe = !this.isMe
			},
			toCheck() {
				if (this.phone.length != 11) {

				} else {
					this.$ajax('/user/sendBindMobileVerifyCode', {
						mobile: this.phone
					}, 'post').then(res => {
						this.getReset = 60
						uni.navigateTo({
							url: './appCheck?phone=' + this.phone
						})
					}).catch(res => {
						uni.showToast({
							title: res.data.obj,
							icon: 'none'
						})
					})
				}
			},
			appIn() {
				if (this.userinfo.phone) {
					this.$ajax('/user/bindMobile', {
						mobile: this.userinfo.phone
					}, 'post').then(res => {
						console.log(res)
					})
				} else {
					uni.showToast({
						title: '暂未获取到手机号，请使用验证码登陆',
						icon: 'none'
					})
				}
			},
			getInfo() {
				this.$ajax('/user/getUserInfo', '', 'get').then(res => {
					// console.log(res)
					this.userInfo = res.data.obj
					this.$store.commit('setUser',this.userInfo)
					this.userInfo.phone = res.data.mobile
					// uni.setStorageSync('userinfo',this.userInfo)
				})
			}
		},
		onShow() {
			uni.getStorageSync('token') ? this.getInfo() : this.getToken()
			// console.log(this.userInfo)
			// switch (plus.os.name) {  
			//         case "Android":  
			//             // 程序全局环境对象  
			//             var mainActivity = plus.android.runtimeMainActivity();  
			//             var Context = new plus.android.importClass("android.content.Context");  
			//             var TelephonyManager = new plus.android.importClass("android.telephony.TelephonyManager");  
			//             var tm = mainActivity.getSystemService(Context.TELEPHONY_SERVICE);  

			//             this.userinfo.msisdn = tm.getLine1Number();  
			//             plus.storage.setItem("msisdn", this.userinfo.msisdn);  

			//             if (this.userinfo.msisdn != "") {  
			//                 this.userinfo.phone = this.userinfo.msisdn;  
			//                 plus.storage.setItem("phone", this.userinfo.msisdn);  
			//             }  
			//             break;  
			//         case "iOS":  
			//             break;  
			//         default:  
			//             break;  
			//     }
		}
	}
</script>
